package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.OverflowList;
import com.atguigu.jxc.entity.OverflowListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.OverflowService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/9/5
 * @Dersciption
 */
@RestController
@RequestMapping("overflowListGoods")
public class OverflowListGoodsController {

    @Autowired
    private OverflowService overflowService;

    /**
     * 保存报溢单
     * @param overflowList
     * @param overflowListGoodsStr
     * @param session
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(OverflowList overflowList, String overflowListGoodsStr, HttpSession session) {
        User currentUser = (User) session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();
        overflowList.setUserId(userId);
        Gson gson = new Gson();
        List<OverflowListGoods> overflowListGoods =
                gson.fromJson(overflowListGoodsStr, new TypeToken<List<OverflowListGoods>>() {
                }.getType());
        Boolean isSave = overflowService.save(overflowList, overflowListGoods);
        if (isSave) {
            return new ServiceVO(100, "请求成功");
        }
        return new ServiceVO(300, "请求失败");
    }

    /**
     * 查询报溢单
     * @param sTime
     * @param eTime
     * @return
     */
    @PostMapping("list")
    public Map<String, Object> list(String sTime, String eTime) {
        Map<String, Object> result = new HashMap<>();
        List<OverflowList> rows = overflowService.list(sTime, eTime);
        result.put("rows",rows);
        return result;
    }

    /**
     * 查询报溢单商品信息
     * @param overflowListId
     * @return
     */
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(Integer overflowListId){
        Map<String, Object> result = new HashMap<>();
        List<OverflowList> rows = overflowService.goodsList(overflowListId);
        result.put("rows",rows);
        return result;
    }
}
